/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemmanager.domain.Poet;
import me.zhengjie.poemmanager.service.PoetService;
import me.zhengjie.poemmanager.domain.vo.PoetQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author hwl
* @date 2024-12-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "poet管理")
@RequestMapping("/api/poet")
public class PoetController {

    private final PoetService poetService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportPoet(HttpServletResponse response, PoetQueryCriteria criteria) throws IOException {
        poetService.download(poetService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询poet")
    @ApiOperation("查询poet")
    @AnonymousAccess
    public ResponseEntity<List<Poet>> queryPoet(PoetQueryCriteria criteria){
        return new ResponseEntity<>(poetService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/paged")
    @Log("分页查询poet")
    @ApiOperation("分页查询poet")
    @AnonymousAccess
    public ResponseEntity<PageResult<Poet>> pagedQueryPoet(PoetQueryCriteria criteria,
                                                           @RequestParam(defaultValue = "1") int current,
                                                           @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(poetService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增poet")
    @ApiOperation("新增poet")
    @AnonymousAccess
    public ResponseEntity<Object> createPoet(@Validated @RequestBody Poet resources){
        poetService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改poet")
    @ApiOperation("修改poet")
    @AnonymousAccess
    public ResponseEntity<Object> updatePoet(@Validated @RequestBody Poet resources){
        poetService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除poet")
    @ApiOperation("删除poet")
    @AnonymousAccess
    public ResponseEntity<Object> deletePoet(@RequestBody List<String> ids) {
        poetService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @GetMapping("/getById")
    @Log("根据主键查询Poet")
    @ApiOperation("根据主键查询用户Poet")
    @AnonymousAccess
    public Poet getPoetById(@RequestParam Integer poetId){

        return poetService.getPoetById(poetId);
    }

}